<?php
class TermController extends Controller{
    /**
     * display all category
     */
    public function actionIndex($vocabulary_id){
        //get all term by vocabulary id
        $model = new Term('search');

        if(isset($_GET['Term']))
            $model->attributes = $_GET['Term'];

        $dataProvider = $model->search($vocabulary_id);
        $vocabulary = Vocabulary::model()->findByPk($vocabulary_id);
        $this->render('//object/term/index', array(
            'model' => $model,
            'vocabulary' => $vocabulary,
            'dataProvider' => $dataProvider
        ));
    }

    /**
     * create new term
     * @param type $vocabulary_id
     */
    public function actionCreate($vocabulary_id){
        $model = new Term;

        if(isset($_POST['Term'])){
            //save
            $model->attributes = $_POST['Term'];
            $model->vocabulary_id = $vocabulary_id;
            if($model->parent_id){
                $parent = Term::model()->findByPk($model->parent_id);
                $model->appendTo($parent);
            }else{
                $model->saveNode();
            }

            Yii::app()->user->setFlash('success', 'Term has been created');

            //mode to update
            $this->redirect('update', array(
                'id' => $model->id
            ));
        }
        $vocabulary = Vocabulary::model()->findByPk($vocabulary_id);
        $this->render('//object/term/create', array(
            'model' => $model,
            'vocabulary' => $vocabulary
        ));
    }

    /**
     * update an term
     * @param type $id
     */
    public function actionUpdate($id){
        $model = Term::model()->findByPk($id);

        if(isset($_POST['Term'])){
            //save
            $model->attributes = $_POST['Term'];

            if($model->parent_id){
                $parent = Term::model()->findByPk($model->parent_id);
                $model->saveNode();
                $model->moveAsLast($parent);
            }else{
                $model->saveNode();
                $model->moveAsRoot();
            }

            Yii::app()->user->setFlash('success', 'Term has been created');
        }

        $vocabulary = Vocabulary::model()->findByPk($model->vocabulary_id);
        $this->render('//object/term/update', array(
            'model' => $model,
            'vocabulary' => $vocabulary
        ));
    }

    public function actionDelete($id){
        //remove term from list
        if(Yii::app()->request->isPostRequest){
            //delete term
            Term::model()->findByPk($id)->deleteNode();
        }

        // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
        if(!isset($_GET['ajax']))
                $this->redirect(array('index'));
    }

    /**
     * this function show all vocabulary from db
     */
    public function actionList(){
        $model = new Vocabulary('search');

        $this->render('//object/vocabulary/index', array(
            'model' => $model
        ));
    }
}